package org.axed.user.client.jsni;

import com.google.gwt.core.client.JavaScriptObject;
import org.axed.user.client.AxedEvent;
import org.axed.user.client.AxedListener;
import org.timepedia.exporter.client.Exportable;

/**
 * An implementation to allow native JS to add listeners.
 * @gwt.exportPackage axed
 * @gwt.export
 */
public class KeyPressListener implements AxedListener, Exportable {
	JavaScriptObject callBack;

	public AxedEvent event;

	public KeyPressListener(JavaScriptObject callBack) {
		this.callBack = callBack;
	}

	public void onAxedEvent(AxedEvent event) {
		if (event.type != AxedEvent.KEY_PRESS) {
			return;
		}
		this.event = event;
		_callBack(event.getKeyPress().ch);	
	}

	public void cancel() {
		this.event.cancel = true;
	}

	private native void _callBack(char ch) /*-{
		var callb = this.@org.axed.user.client.jsni.KeyPressListener::callBack;
		callb(ch);
	}-*/;

}
